<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 头部 -->
      <PageTools :show-before="true">
        <span slot="before"> 本月：入职 离职 调薪 未定薪 </span>
        <div slot="after" type="flex" justify="between">
          <el-button @click="$router.push({path:'/salarys/wagesetting'})">设置</el-button>
          <el-button @click="$router.push(`/salarys/monthStatement?yearMonth=${yearMonth}`)">202006月报表</el-button>
        </div>
      </PageTools>

      <!-- 导航(筛选) -->
      <el-card>
        <el-form>
          <el-form-item>
            <span>聘用形式：</span>
            <el-checkbox label="正式" />
            <el-checkbox label="非正式" />
          </el-form-item>
          <el-form-item>
            <span>员工状态:</span>
            <el-checkbox label="在职" />
            <el-checkbox label="离职" />
          </el-form-item>
          <el-form-item>
            <span>部门:</span>
            <el-checkbox-group v-model="checkList">
              <el-checkbox
                v-for="item in depList"
                :key="item.id"
                :label="item.name"
              >{{ item.name }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 表格数据 -->
      <el-card>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="100" />
          <el-table-column prop="username" label="姓名" width="100" />
          <el-table-column prop="mobile" label="手机" width="150" />
          <el-table-column prop="formOfEmployment" :formatter="formatterTable" label="聘用形式" width="100" />
          <el-table-column prop="departmentName" label="部门" width="100" />
          <el-table-column prop="timeOfEntry" label="入职时间" width="150">
            <!-- 过滤时间  -->
            <template slot-scope="scope">
              {{ scope.row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column prop="currentBasicSalary" label="工资基数" width="100">
            <template slot-scope="scope">
              {{ scope.row.currentBasicSalary+scope.row.currentPostWage }}
            </template>
          </el-table-column>
          <el-table-column prop="currentPostWage" label="津贴方案" width="100">
            {{ money.subsidyName }}
          </el-table-column>
          <el-table-column prop="name" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="small" :type="scope.row.currentBasicSalary+scope.row.currentPostWage>0 ?'primary':'danger'" @click="showDialog(scope.row.id,scope.row.currentBasicSalary+scope.row.currentPostWage>0?'调薪':'定薪')">{{ scope.row.currentBasicSalary+scope.row.currentPostWage>0?'调薪':'定薪' }}</el-button>
              <el-button size="small" @click="$router.push('/salarys/lookView/'+scope.row.id)">查看</el-button>
            </template>
            <!-- <el-button size="small" type="primary">调薪</el-button> -->
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-row class="block" type="flex" justify="end">
          <el-pagination
            background
            :page-sizes="[10, 20, 30, 40]"
            :page-size="100"
            :page="page.page"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </el-row>
      </el-card>

    </div>

    <!-- 调薪 弹窗 -->
    <el-dialog v-if="dia==='调薪'" title="调薪" :visible="dialogFormVisible" @close="btnCancel">
      <div class="fl headL">
        <div class="headImg">
          <img v-imgerr="require('@/assets/common/img.jpeg')" :src="staffPhoto">
        </div>
        <div class="headInfoTip">
          <P>{{ userList.username }}</P>
          <P>部门：{{ userList.departmentName }}</P>
          <P>入职时间：{{ userList.timeOfEntry | formatDate }}</P>
        </div>
      </div>
      <hr>
      <el-form :model="form">
        <el-form-item label="调整基本工资" label-width="80px">
          <el-input :value="form.currentBasicSalary" :disabled="true" style="width:65%" auto-complete="off" /><br>
          <el-input v-model="newcurrentBasicSalary" style="width:65%" auto-complete="off" />
        </el-form-item>
        <el-form-item label="调整岗位工资" label-width="80px">
          <el-input :value="form.currentPostWage" :disabled="true" style="width:65%" auto-complete="off" /><br>
          <el-input v-model="newcurrentPostWage" style="width:65%" auto-complete="off" />
        </el-form-item>
        <el-form-item label="工资合计" label-width="80px">
          <el-input :value="form.currentBasicSalary+form.currentPostWage" :disabled="true" style="width:65%" auto-complete="off" /><br>
          <el-input :disabled="true" :value="(+newcurrentBasicSalary+ +newcurrentPostWage)" style="width:65%" auto-complete="off" />
        </el-form-item>
        <el-form-item label="调整幅度" label-width="80px">
          <el-input :disabled="true" :value="(+newcurrentBasicSalary+ +newcurrentPostWage) - (form.currentBasicSalary+ form.currentPostWage)" style="width:65%" auto-complete="off" />
          <br></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="btnOk">保存</el-button>
        <el-button @click="btnCancel">关闭</el-button>
      </div>
    </el-dialog>

    <!-- 定薪 -->
    <el-dialog v-else title="定薪" :visible="dialogFormVisible" @close="btnCan">
      <el-form ref="okList" :model="okList" label-width="130px" label-position="left">
        <el-form-item label="当前基本工资">
          <el-input v-model="okList.correctionOfBasicWages" style="width:65%" placeholder="当前基本工资" />
        </el-form-item>
        <el-form-item label="当前岗位工资">
          <el-input v-model="okList.currentBasicSalary" style="width:65%" placeholder="当前岗位工资" />
        </el-form-item>
        <el-form-item label="当前工资合计">
          <el-input :value="okList.currentPostWage=Number(okList.correctionOfBasicWages)+Number(okList.currentBasicSalary)" style="width:65%" :disabled="true" />
        </el-form-item>
        <el-form-item label="转正基本工资">
          <el-input v-model="okList.fixedBasicSalary" style="width:65%" placeholder="转正基本工资" />
        </el-form-item>
        <el-form-item label="转正岗位工资">
          <el-input v-model="okList.fixedPostWage" style="width:65%" placeholder="转正岗位工资" />
        </el-form-item>
        <el-form-item label="转正工资合计">
          <el-input :value="okList.turnToPostWages=Number(okList.fixedBasicSalary)+Number(okList.fixedPostWage)" style="width:65%" :disabled="true" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="btnSubmit">确认</el-button>
          <el-button @click="btnCan">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getSalaryssetting } from '@/api/salarys'
import { mapGetters } from 'vuex'
// 引入枚举数据
import employmentEnum from '@/api/constant/employees'

import { getDepartments } from '@/api/departments'
import { getSalarys, getChangeSalarys, changeSalarys, okSalarys, getsettingMonth } from '@/api/salarys'
import { getUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userId: '',
      userList: {},
      form: {},
      dia: '',
      money: {},
      yearMonth: '',
      newcurrentBasicSalary: '',
      newcurrentPostWage: '',
      dialogFormVisible: false, // 弹窗
      depList: [], // 部门列表
      checkList: [], // 多选框
      tableData: [], // 列表数据
      okList: {
        correctionOfBasicWages: '', // 转正基本工资
        currentBasicSalary: '', // 当前基本工资
        currentPostWage: '', // 当前岗位工资
        fixedBasicSalary: '', // 定薪基本工资
        fixedPostWage: '', // 定薪岗位工资
        turnToPostWages: '', // 转正岗位工资
        userId: '' // 用户id
      }, // 定资
      page: {
        page: 1, // 当前页码
        pageSize: 10,
        total: 10
      }
    }
  },
  computed: {
    ...mapGetters([
      'staffPhoto'
    ])
  },
  watch: {

    // 筛选
    checkList() {
      // console.log(typeof (this.checkList))
      if (this.checkList.length === 0) {
        // 获取工资列表
        this.getSalarys()
      }
      this.tableData = this.tableData.filter((item) => {
        return this.checkList.find(i => {
          return item.departmentName === i
        })
      })
      console.log(typeof (this.checkList))
      console.log(this.tableData)
    }
  },
  mounted() {
    // 获取部门列表
    getDepartments().then((res) => {
      console.log(res)
      this.depList = res.depts
      console.log(this.depList)
    })
    // 获取工资列表
    this.getSalarys()
    this.getSalaryssetting()
    this.getsettingMonth()
  },

  methods: {
    // 获取企业是否设置当月工资
    async getsettingMonth() {
      this.settingMonthList = await getsettingMonth()
      console.log(this.settingMonthList)
      this.yearMonth = this.settingMonthList.dataMonth
      console.log(this.yearMonth)
      // console.log(this.settingMonthList.dataMonth)
      // // // 根据年月查询企业用户薪资列表
      // this.reportsList = await reports(Number(202005))
      // console.log(this.reportsList)
    },
    async getSalaryssetting() {
      this.money = await getSalaryssetting()
      console.log(this.money)
    },
    // 定薪 取消
    btnCan() {
      this.dialogFormVisible = false
      // 清空表单数据
      this.okList = {
        correctionOfBasicWages: '', // 转正基本工资
        currentBasicSalary: '', // 当前基本工资
        currentPostWage: '', // 当前岗位工资
        fixedBasicSalary: '', // 定薪基本工资
        fixedPostWage: '', // 定薪岗位工资
        turnToPostWages: '', // 转正岗位工资
        userId: '' // 用户id
      }
      this.dialogFormVisible = false
    },
    // 定薪 确认
    async btnSubmit() {
      console.log(this.okList)
      await okSalarys(this.okList)
      this.$message.success('定薪成功')
      this.dialogFormVisible = false
      // 获取工资列表
      this.getSalarys()
    },
    // 调薪 取消
    btnCancel() {
      this.dialogFormVisible = false
      // 清空表单数据
      this.form = {}
      this.newcurrentBasicSalary = ''
      this.newcurrentPostWage = ''
    },
    // 调薪 保存
    async btnOk() {
      // console.log(this.newcurrentBasicSalary)
      console.log(this.form)
      this.form.currentBasicSalary = +this.newcurrentBasicSalary
      this.form.currentPostWage = +this.newcurrentPostWage
      // console.log(this.form)
      // 更新调薪
      const res = await changeSalarys(this.userList.id, this.form)
      console.log(res)
      // 重新获取数据
      this.getSalarys()
      this.newcurrentBasicSalary = ''
      this.newcurrentPostWage = ''
      // 关闭弹窗
      this.dialogFormVisible = false
    },

    // 调薪 弹窗
    async showDialog(userId, data) {
      this.dia = data
      this.okList.userId = userId
      this.dialogFormVisible = true
      // 获取用户详情数据
      this.userList = await getUserDetailById(userId)
      console.log(this.userList)
      // 获取调薪数据
      this.form = await getChangeSalarys(userId)
      console.log(this.form)
    },
    // 封装获取工资列表
    async getSalarys() {
      const { rows, total } = await getSalarys(this.page)
      this.tableData = rows
      this.page.total = total
      console.log(this.tableData)
      // console.log(this.tableData.id)
    },
    // 格式化聘用形式
    formatterTable(row, column, cellValue, index) {
      // 查找1所对应的值
      const arr = employmentEnum.hireType.find(item => item.id === cellValue)
      // console.log(arr)
      return arr ? arr.value : '未知'
    },
    // current-change 当前页
    handleCurrentChange(value) {
      // console.log(value)
      this.page.page = value
      // 获取工资列表
      this.getSalarys()
    },
    // 每页显示几条
    handleSizeChange(value) {
      // console.log(value)
      this.page.pageSize = value
      // 获取工资列表
      this.getSalarys()
    }

  }
}
</script>

<style lang="scss" scoped>
.top {
  width: 40%;
  display: flex;
  justify-content: space-between;
}
  .headImg {
    float: left;
    width: 100px;
      margin-right: 30px;
    height: 100px;
    border-radius: 50%;
    background: #999;
          img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      border: 0.5px solid #ccc;
    }
  }
  .headL{
    height: 120px;
  }
</style>
